/**
 * 问卷系统类型定义
 */

// 题目类型枚举
export enum QuestionType {
  SINGLE_CHOICE = 'single_choice',
  MULTIPLE_CHOICE = 'multiple_choice',
  TRUE_FALSE = 'true_false',
  FILL_BLANK = 'fill_blank',
  COMPOSITE = 'composite',
}

// 难度等级
export enum DifficultyLevel {
  VERY_EASY = 1,
  EASY = 2,
  MEDIUM = 3,
  HARD = 4,
  VERY_HARD = 5,
}

// 试卷状态
export enum PaperStatus {
  DRAFT = 'draft',
  PUBLISHED = 'published',
  ENDED = 'ended',
}

// 提交状态
export enum SubmissionStatus {
  IN_PROGRESS = 'in_progress',
  SUBMITTED = 'submitted',
  GRADED = 'graded',
}

// 选项接口
export interface Choice {
  key: string;
  content: string;
  has_code?: boolean;
  code?: string;
  language?: string;
}

// 题库接口
export interface QuestionBank {
  id: number;
  name: string;
  description: string;
  created_by: number;
  created_by_name: string;
  create_time: string;
  update_time: string;
  is_public: boolean;
  question_count: number;
}

// 题目列表项接口
export interface QuestionListItem {
  id: number;
  title: string;
  type: QuestionType;
  type_display: string;
  difficulty: DifficultyLevel;
  tags: string[];
  question_bank?: number;
  question_bank_name?: string;
  created_by_name: string;
  create_time: string;
  is_active: boolean;
  use_count: number;
  correct_rate: number;
}

// 子题目接口
export interface SubQuestion {
  id?: number;
  content: string;
  type: QuestionType;
  type_display?: string;
  sequence: number;
  choices: Choice[];
  correct_answers: string[];
  blank_count?: number;
  case_sensitive?: boolean;
  has_code?: boolean;
  code_content?: string;
  code_language?: string;
  answer_explanation?: string;
  hint?: string;
}

// 题目详情接口
export interface Question {
  id?: number;
  title: string;
  content: string;
  type: QuestionType;
  type_display?: string;
  question_bank?: number;
  question_bank_name?: string;
  difficulty: DifficultyLevel;
  tags: string[];
  parent?: number;
  sequence?: number;
  choices: Choice[];
  correct_answers: string[];
  blank_count?: number;
  case_sensitive?: boolean;
  has_code?: boolean;
  code_content?: string;
  code_language?: string;
  answer_explanation?: string;
  hint?: string;
  created_by?: number;
  created_by_name?: string;
  create_time?: string;
  update_time?: string;
  is_active?: boolean;
  source?: string;
  use_count?: number;
  correct_rate?: number;
  sub_questions?: SubQuestion[];
}

// 试卷题目关联接口
export interface PaperQuestion {
  id?: number;
  question: number;
  question_detail?: Question;
  sequence: number;
  group_name?: string;
  score: number;
  is_required?: boolean;
}

// 试卷列表项接口
export interface PaperListItem {
  id: number;
  title: string;
  description: string;
  status: PaperStatus;
  status_display: string;
  is_public: boolean;
  duration: number;
  start_time?: string;
  end_time?: string;
  total_score: number;
  passing_score: number;
  created_by_name: string;
  create_time: string;
  update_time: string;
  submit_count: number;
  question_count: number;
}

// 试卷详情接口
export interface Paper {
  id?: number;
  title: string;
  description: string;
  cover_image?: string;
  status: PaperStatus;
  status_display?: string;
  is_public: boolean;
  duration: number;
  start_time?: string;
  end_time?: string;
  total_score: number;
  passing_score: number;
  show_score_after_submit: boolean;
  show_answer_after_submit: boolean;
  show_analysis_after_submit: boolean;
  random_question_order: boolean;
  random_choice_order: boolean;
  allow_multiple_submit: boolean;
  max_submit_count: number;
  created_by?: number;
  created_by_name?: string;
  create_time?: string;
  update_time?: string;
  submit_count?: number;
  paper_questions?: PaperQuestion[];
}

// 答题记录接口
export interface AnswerRecord {
  id: number;
  question: number;
  question_detail?: QuestionListItem;
  user_answer: any;
  is_correct: boolean;
  score: number;
  score_ratio: number;
  answer_time: string;
  time_spent: number;
}

// 提交记录列表项接口
export interface SubmissionListItem {
  id: number;
  paper: number;
  paper_title: string;
  user: number;
  user_name: string;
  start_time: string;
  submit_time?: string;
  status: SubmissionStatus;
  status_display: string;
  total_score: number;
  score_ratio: number;
  correct_count: number;
  total_count: number;
}

// 提交记录详情接口
export interface Submission {
  id: number;
  paper: number;
  paper_detail?: PaperListItem;
  user: number;
  user_name: string;
  start_time: string;
  submit_time?: string;
  status: SubmissionStatus;
  status_display: string;
  total_score: number;
  score_ratio: number;
  correct_count: number;
  total_count: number;
  submit_count: number;
  answer_records?: AnswerRecord[];
}

// 提交答案接口
export interface SubmitAnswer {
  question_id: number;
  answer: any;
  time_spent: number;
}

// 评论接口
export interface QuestionComment {
  id?: number;
  question: number;
  user?: number;
  user_name?: string;
  content: string;
  parent?: number;
  create_time?: string;
  update_time?: string;
  like_count?: number;
  replies?: QuestionComment[];
}

// 题目统计接口
export interface QuestionStatistics {
  total: number;
  by_type: Record<string, number>;
  by_difficulty: Record<number, number>;
}

// 试卷统计接口
export interface PaperStatistics {
  total_submissions: number;
  avg_score: number;
  avg_score_ratio: number;
  pass_count: number;
  score_distribution: {
    '0-60': number;
    '60-70': number;
    '70-80': number;
    '80-90': number;
    '90-100': number;
  };
}

// 创建试卷请求接口
export interface CreatePaperRequest {
  title: string;
  description: string;
  cover_image?: string;
  status?: PaperStatus;
  is_public?: boolean;
  duration?: number;
  start_time?: string;
  end_time?: string;
  total_score?: number;
  passing_score?: number;
  show_score_after_submit?: boolean;
  show_answer_after_submit?: boolean;
  show_analysis_after_submit?: boolean;
  random_question_order?: boolean;
  random_choice_order?: boolean;
  allow_multiple_submit?: boolean;
  max_submit_count?: number;
  questions_data?: Array<{
    question_id: number;
    sequence: number;
    group_name?: string;
    score: number;
    is_required?: boolean;
  }>;
}

// 创建题目请求接口
export interface CreateQuestionRequest {
  title?: string;
  content: string;
  type: QuestionType;
  question_bank?: number;
  difficulty?: DifficultyLevel;
  tags?: string[];
  parent?: number;
  sequence?: number;
  choices?: Choice[];
  correct_answers?: string[];
  blank_count?: number;
  case_sensitive?: boolean;
  has_code?: boolean;
  code_content?: string;
  code_language?: string;
  answer_explanation?: string;
  hint?: string;
  is_active?: boolean;
  source?: string;
  sub_questions_data?: Array<Omit<SubQuestion, 'id' | 'type_display'>>;
}
